import { Vec3 } from '@/core/types';
import { WorldManager } from '@/world/WorldManager';
import { PLAYER_HEIGHT, PLAYER_WIDTH } from '@/core/constants';

export class CollisionSystem {
  private worldManager: WorldManager;

  constructor(worldManager: WorldManager) {
    this.worldManager = worldManager;
  }

  public checkCollision(position: Vec3, velocity: Vec3, deltaTime: number): Vec3 {
    const newPosition = { ...position };
    
    // X轴碰撞检测
    newPosition.x += velocity.x * deltaTime;
    if (this.isColliding(newPosition)) {
      newPosition.x = position.x;
    }
    
    // Y轴碰撞检测
    newPosition.y += velocity.y * deltaTime;
    if (this.isColliding(newPosition)) {
      newPosition.y = position.y;
      velocity.y = 0;
    }
    
    // Z轴碰撞检测
    newPosition.z += velocity.z * deltaTime;
    if (this.isColliding(newPosition)) {
      newPosition.z = position.z;
    }
    
    return newPosition;
  }

  private isColliding(position: Vec3): boolean {
    const halfWidth = PLAYER_WIDTH / 2;
    const minX = position.x - halfWidth;
    const maxX = position.x + halfWidth;
    const minY = position.y;
    const maxY = position.y + PLAYER_HEIGHT;
    const minZ = position.z - halfWidth;
    const maxZ = position.z + halfWidth;
    
    // 检查玩家边界框内的所有方块
    for (let x = Math.floor(minX); x <= Math.floor(maxX); x++) {
      for (let y = Math.floor(minY); y <= Math.floor(maxY); y++) {
        for (let z = Math.floor(minZ); z <= Math.floor(maxZ); z++) {
          const blockType = this.worldManager.getBlock({ x, y, z });
          if (blockType > 0) { // 非空气方块
            return true;
          }
        }
      }
    }
    
    return false;
  }

  public isOnGround(position: Vec3): boolean {
    const halfWidth = PLAYER_WIDTH / 2;
    const groundY = position.y - 0.1;
    
    for (let x = Math.floor(position.x - halfWidth); x <= Math.floor(position.x + halfWidth); x++) {
      for (let z = Math.floor(position.z - halfWidth); z <= Math.floor(position.z + halfWidth); z++) {
        const blockType = this.worldManager.getBlock({ x, y: Math.floor(groundY), z });
        if (blockType > 0) {
          return true;
        }
      }
    }
    
    return false;
  }

  public wouldBlockCollideWithPlayer(blockPosition: Vec3, playerPosition: Vec3): boolean {
    const halfWidth = PLAYER_WIDTH / 2;
    const playerMinX = playerPosition.x - halfWidth;
    const playerMaxX = playerPosition.x + halfWidth;
    const playerMinY = playerPosition.y;
    const playerMaxY = playerPosition.y + PLAYER_HEIGHT;
    const playerMinZ = playerPosition.z - halfWidth;
    const playerMaxZ = playerPosition.z + halfWidth;
    
    // 检查方块是否与玩家边界框重叠
    const blockMinX = blockPosition.x;
    const blockMaxX = blockPosition.x + 1;
    const blockMinY = blockPosition.y;
    const blockMaxY = blockPosition.y + 1;
    const blockMinZ = blockPosition.z;
    const blockMaxZ = blockPosition.z + 1;
    
    // AABB碰撞检测
    return !(blockMaxX <= playerMinX || blockMinX >= playerMaxX ||
             blockMaxY <= playerMinY || blockMinY >= playerMaxY ||
             blockMaxZ <= playerMinZ || blockMinZ >= playerMaxZ);
  }
}